angular.module('elink', ['ionic', 'angular-meteor','ngCordova','global','gateway'])
.config(function($stateProvider,$urlRouterProvider,$ionicConfigProvider) {
  $ionicConfigProvider.backButton.text('');
  $ionicConfigProvider.backButton.previousTitleText(false);

  // 根路由，判断用户登录
  $urlRouterProvider.when('',function($rootScope,$state,$ionicPopup){
    if($rootScope.loggingIn){
      $rootScope.$watch('currentUser', function(newValue, oldValue) {
        if(newValue){
          // setTimeout(function(){
            if(newValue.profile.roleName=="老板"){
              window.location.href='ceo.html';
            }else if(newValue.profile.roleName=="店长"){
              window.location.href='manager.html';
            }else if(newValue.profile.roleName=="销售员"){  // && firstLogin=true
              window.location.href='seller.html';
              // window.location.href='#/pwmodify';
            }else if(newValue.profile.roleName=="仓管员"){  // && firstLogin=true
              window.location.href='whm.html';
              // window.location.href='#/pwmodify';
            }else if(newValue.profile.roleName=="收银员"){  // && firstLogin=true
              window.location.href='casher.html';
              // window.location.href='#/pwmodify';
            }else{
              $ionicPopup.alert({
                title: '系统消息',
                template: '<p class="text-center">尚未授权登陆</p>', 
                cancelText: '',
                okText: '退出',
                okType: 'button-royal'
              }).then(function (res) {
                ionic.Platform.exitApp();
              });
            }
        }
      }); //检查被监控的对象的每个属性是否发生变化
    }else{
        $state.go('elink.gateway');
    }
  });

  $stateProvider.state('elink', {
    url: '/',
    abstract: true,
    templateUrl: 'templates/common/app.html',
    controller: 'AppCtrl'
  });
})

.run(function($cordovaToast, $ionicPlatform,CheckUpdateService,StatusBarService,BackButtonService) {
  $ionicPlatform.ready(function() {
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
      CheckUpdateService.checkUpdate();
    }

    StatusBarService.setStyle();
    BackButtonService.registerBackButtonAction();
  });
})

.controller('AppCtrl', function($scope,$state) {
 
});
